package com.mobile.challenge.ui.report.unit.manager.weekmonth

import android.app.Application
import android.graphics.Color
import androidx.lifecycle.MutableLiveData
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.mobile.challenge.api.ManagerMonthDataService
import com.mobile.challenge.bean.ManagerWeekMonthDataBean
import com.mobile.challenge.exception.MobileException
import com.mobile.challenge.ui.report.model.PeriodType
import com.mobile.challenge.ui.report.utils.IntegerFormat
import com.mobile.challenge.user.UserHelper
import com.mobile.challenge.util.DateUtil
import com.mobile.challenge.util.SUCCESS_ERROR_CODE
import com.mobile.challenge.util.SUCCESS_ERROR_MSG
import com.mobile.frame.mvvmframe.base.BaseModel
import com.mobile.frame.mvvmframe.base.DataViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList

class ManagerLineViewModel @Inject constructor(application: Application, model: BaseModel?) : DataViewModel(application, model) {
    var lineData = MutableLiveData<LineData>()

    var dataList: List<ManagerWeekMonthDataBean> = ArrayList(0)

    private var periodType = 0

    fun generateData(periodType: Int, calendar: Calendar) {
        this.periodType = periodType
        getData(calendar)
    }

    private fun getData(calendar: Calendar) {
        val formatDateStr = if (periodType == PeriodType.WEEKLY) {
            DateUtil.getFormatWeekStartData(calendar)
        } else {
            DateUtil.getFormatMonthStartData(calendar)
        }
        val subscribe = getRetrofitService(ManagerMonthDataService::class.java)
                .getData(UserHelper.getInstance().getUserId(),
                        formatDateStr,
                        DateUtil.getFormatDateStr(calendar))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ response ->
                    if (response.errorCode == SUCCESS_ERROR_CODE && response.errorMsg == SUCCESS_ERROR_MSG) {
                        val data = response.data
                        dataList = data
                        // 数据
                        val lineNA = ArrayList<Entry>(0)
                        val lineOffice = ArrayList<Entry>(0)
                        val lineVacation = ArrayList<Entry>(0)
                        val lineWFH = ArrayList<Entry>(0)
                        for (i in 0 until data.size) {
                            val weekMonthDataBean = data[i]
                            lineNA.add(Entry(i.toFloat(), weekMonthDataBean.group.nacount.toFloat()))
                            lineOffice.add(Entry(i.toFloat(), weekMonthDataBean.group.officeCount.toFloat()))
                            lineVacation.add(Entry(i.toFloat(), weekMonthDataBean.group.leaveCount.toFloat()))
                            lineWFH.add(Entry(i.toFloat(), weekMonthDataBean.group.workHomeCount.toFloat()))
                        }

                        val lineDataSetNA = LineDataSet(lineNA, "NA")
                        initLineDataSet(lineDataSetNA, Color.parseColor("#f38181"), LineDataSet.Mode.LINEAR)

                        val lineDataSetOffice = LineDataSet(lineOffice, "Office")
                        initLineDataSet(lineDataSetOffice, Color.parseColor("#fce38a"), LineDataSet.Mode.LINEAR)

                        val lineDataSetVacation = LineDataSet(lineVacation, "Vacation")
                        initLineDataSet(lineDataSetVacation, Color.parseColor("#eaffd0"), LineDataSet.Mode.LINEAR)

                        val lineDataSetWFH = LineDataSet(lineWFH, "WFH")
                        initLineDataSet(lineDataSetWFH, Color.parseColor("#95e1d3"), LineDataSet.Mode.LINEAR)

                        val lineData1 = LineData(lineDataSetNA, lineDataSetOffice, lineDataSetVacation, lineDataSetWFH)
                        lineData1.setValueFormatter(IntegerFormat())
                        lineData.value = lineData1

                    } else {
                        val exception = MobileException()
                        exception.errorCode = response.errorCode
                        exception.errorMsg = response.errorMsg
                    }

                }, {
                    val exception = MobileException()
                    exception.errorCode = "00000"
                    exception.errorMsg = "response.errorMsg"
                })
    }


    private fun initLineDataSet(lineDataSet: LineDataSet, color: Int, mode: LineDataSet.Mode?) {
        lineDataSet.setColor(color)
        lineDataSet.setCircleColor(color)
        lineDataSet.setLineWidth(1f)
        lineDataSet.setCircleRadius(3f)
        //设置曲线值的圆点是实心还是空心
        lineDataSet.setDrawCircleHole(false)
        lineDataSet.setValueTextSize(10f)
        //设置折线图填充
        lineDataSet.setDrawFilled(true)
        lineDataSet.setFormLineWidth(1f)
        lineDataSet.setFormSize(15f)
        if (mode == null) {
            //设置曲线展示为圆滑曲线（如果不设置则默认折线）
            lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER)
        } else {
            lineDataSet.setMode(mode)
        }
    }

    fun getDataStr(position: Int): String {
        if (dataList != null) {
            return dataList[position].date
        }
        return ""
    }
}